package unittest;
import java.sql.SQLException;
import java.util.Random;

import junit.framework.TestCase;
import server.EchoServer;
import server.mysqlConnection;
import control.ReportsControl;


/**
 * @author Shay
 *  Testing functions on the server in the operation Report
 */
public class ReportsTest extends TestCase 
{
	private ReportsControl report;
	
	
	
	/**
	 * Setup for testing
	 */
	public void setup()
	{
		report=new ReportsControl();
		try 
		{
			EchoServer.connectionToDB=mysqlConnection.openConnection("remote.roeeee.com","root","1234","test");
		} 
		catch (SQLException e1) 
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	
	
	/**
	 * Testing the operation Number  Active Subscriber
	 */
	public void testNumberActiveSubscriber() 
	{
		setup();
		String rst=mysqlConnection.check("SELECT COUNT(sid)  FROM subscriber WHERE CardStatus=1;");
		int count=Integer.parseInt(rst);
		//ReportsControl report=new ReportsControl();
		int x=Integer.parseInt(report.numberOfActive());
		assertEquals(count,x);
		Boolean contine=false;
		int sid = 0;
		do
		{
			try
			{
				contine=false;
				Random generator=new Random();
				sid=generator.nextInt(10000);
				String user=String.valueOf(sid);
				String UpdateSubscriberDB=new String("INSERT into test.subscriber SET "+"sId="+"'"+ user+"'"+",CardStatus=1"+", GraduateDate='3000/12/12';");
				mysqlConnection.update(UpdateSubscriberDB);
			}
			catch(Exception e)
			{
				contine=true;
			}
		}
		while(contine);		
		x=Integer.parseInt(report.numberOfActive());
		mysqlConnection.update("DELETE FROM test.subscriber WHERE sid="+sid+";");
		assertEquals(count+1,x);
	}
	
	/**
	 * Testing the operation Number Freeze Subscriber
	 */
	public void testNumberFreezeSubscriber() 
	{
		setup();
		String rst=mysqlConnection.check("SELECT COUNT(sid)  FROM subscriber WHERE CardStatus=2;");
		int count=Integer.parseInt(rst);
		
		int x=Integer.parseInt(report.numberOfFrozen());
		assertEquals(count,x);
		Boolean contine=false;
		int sid = 0;
		do
		{
			try
			{
				contine=false;
				Random generator=new Random();
				sid=generator.nextInt(10000);
				String user=String.valueOf(sid);
				String UpdateSubscriberDB=new String("INSERT into test.subscriber SET "+"sId="+"'"+ user+"'"+",CardStatus=2"+", GraduateDate='3000/12/12';");
				mysqlConnection.update(UpdateSubscriberDB);
			}
			catch(Exception e)
			{
				contine=true;
			}
		}
		while(contine);		
		x=Integer.parseInt(report.numberOfFrozen());
		mysqlConnection.update("DELETE FROM test.subscriber WHERE sid="+sid+";");
		assertEquals(count+1,x);
	}
	
	/**
	 * Testing the operation Number Non Active Subscriber
	 */
	public void testNumberNonActiveSubscriber() 
	{
		setup();
		String rst=mysqlConnection.check("SELECT COUNT(sid)  FROM subscriber WHERE CardStatus=0;");
		int count=Integer.parseInt(rst);
		int x=Integer.parseInt(report.numberOFNonActive());
		assertEquals(count,x);
		Boolean contine=false;
		int sid = 0;
		do
		{
			try
			{
				contine=false;
				Random generator=new Random();
				sid=generator.nextInt(10000);
				String user=String.valueOf(sid);
				String UpdateSubscriberDB=new String("INSERT into test.subscriber SET "+"sId="+"'"+ user+"'"+",CardStatus=0"+", GraduateDate='3000/12/12';");
				mysqlConnection.update(UpdateSubscriberDB);
			}
			catch(Exception e)
			{
				contine=true;
			}
		}
		while(contine);		
		x=Integer.parseInt(report.numberOFNonActive());
		mysqlConnection.update("DELETE FROM test.subscriber WHERE sid="+sid+";");
		assertEquals(count+1,x);
	}
	
	/**
	 * Testing the operation Number Lates Books
	 */
	public void testNumberLatesBooks() 
	{
		setup();
		String rst=mysqlConnection.check("SELECT COUNT(sid)  FROM borrow WHERE incident='late';");
		int count=Integer.parseInt(rst);
		int x=Integer.parseInt(report.numberOfLates());
		assertEquals(count,x);
		Boolean contine=false;
		int sid = 0;
		do
		{
			try
			{
				contine=false;
				Random generator=new Random();
				sid=generator.nextInt(10000);
				String user=String.valueOf(sid);
				mysqlConnection.update("INSERT into books SET bId=0, copy=0,name=0, author=0, category=0, description=0, location=0, ispopular=0, isborrowed=0, pathindex=0, expectedReturnDate='3000/12/12', edition=0, printDate=0, purchaseDate='3000/12/12';");
				String UpdateSubscriberDB=new String("INSERT into borrow SET "+"sId="+"'"+ user+"'"+", bid=0"+", borrowDate='3000/12/12', expectedReturnDate='3000/12/12', copy=0 ,incident='Late' ;");
				mysqlConnection.update(UpdateSubscriberDB);
			}
			catch(Exception e)
			{
				contine=true;
			}
		}
		while(contine);		
		x=Integer.parseInt(report.numberOfLates());
		mysqlConnection.update("DELETE FROM borrow WHERE sid="+sid+" and borrowDate='3000/12/12'"+";");
		mysqlConnection.update("DELETE FROM books where bid=0");
		assertEquals(count+1,x);
	}
	/**
	 * Testing the operation NumberOfBorrowedBooks
	 */
	public void testNumberOfBorrowedBooks() 
	{
		setup();
		String rst=mysqlConnection.check("SELECT COUNT(bid)  FROM books WHERE isborrowed=1;");
		int count=Integer.parseInt(rst);
		int x=Integer.parseInt(report.numberOfBorrowedBooks());
		assertEquals(count,x);
		Boolean contine=false;
		int sid = 0;
		do
		{
			try
			{
				contine=false;
				Random generator=new Random();
				sid=generator.nextInt(10000);
				String user=String.valueOf(sid);
				mysqlConnection.update("INSERT into books SET bId=0, copy=0,name=0, author=0, category=0, description=0, location=0, ispopular=0, isborrowed=1, pathindex=0, expectedReturnDate='3000/12/12', edition=0, printDate=0, purchaseDate='3000/12/12';");
			}
			catch(Exception e)
			{
				contine=true;
			}
		}
		while(contine);		
		x=Integer.parseInt(report.numberOfBorrowedBooks());
		mysqlConnection.update("DELETE FROM books where bid=0");
		assertEquals(count+1,x);
	}
	
	
	
	
}
